#
# Ronin Exploits - A Ruby library for Ronin that provides exploitation and
# payload crafting functionality.
#
# Copyright (c) 2007-2013 Hal Brodigan (postmodern.mod3 at gmail.com)
#
# This file is part of Ronin Exploits.
#
# Ronin is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ronin is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ronin.  If not, see <http://www.gnu.org/licenses/>
#

require 'ronin/exploits/exceptions/target_data_missing'
require 'ronin/formatting/binary'

module Ronin
  module Exploits
    module Helpers
      #
      # Adds methods to exploits for formatting binary data.
      #
      module Binary
        UINTS = {
          little: {
            1 => :uchar,
            2 => :uint16_le,
            4 => :uint32_le,
            8 => :uint64_le
          },

          big: {
            1 => :uchar,
            2 => :uint16_be,
            4 => :uint32_be,
            8 => :uint64_be
          }
        }

        #
        # Packs an integer using the current targeted architecture
        # and the address-length.
        #
        # @param [Integer] integer
        #   The integer to pack.
        #
        # @param [Symbol] type
        #   The optional type to pack the integer as.
        #
        # @return [String]
        #   The packed integer.
        #
        # @see http://ronin-ruby.github.com/docs/ronin-support/Ronin/Binary/Template.html
        #
        def pack(integer,type=nil)
          test_arch!

          type ||= UINTS.fetch(arch.endian.to_sym).fetch(arch.address_length)

          return integer.pack(type)
        end
      end
    end
  end
end
